package cn.luoshaocheng.pet.controller;

import cn.luoshaocheng.basic.util.LoginContext;
import cn.luoshaocheng.basic.util.PageList;
import cn.luoshaocheng.basic.util.jsonResult;



import cn.luoshaocheng.pet.domain.Pet;
import cn.luoshaocheng.pet.query.PetQuery;
import cn.luoshaocheng.pet.service.IPetService;
import cn.luoshaocheng.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService PetService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping

    public List<Pet> loadAll(HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return PetService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Pet loadById(@PathVariable("id") Long id) {
        return PetService.loadById(id);
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @DeleteMapping("/{id}")
    public jsonResult deleteById(@PathVariable("id") Long eid) {
        try {
            PetService.delete(eid);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param Pet
     * @return
     */
    @PutMapping
    public jsonResult addAndUpdate(@RequestBody Pet Pet) {
        try {
            if (Pet.getId() == null) {
                PetService.save(Pet);
            }else{
                PetService.update(Pet);
            }
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param PetQuery
     * @return
     */
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery PetQuery) {
        return PetService.queryPage(PetQuery);

    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping
    public jsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            PetService.patchDel(ids);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }
    @PostMapping("/onsale")
    public jsonResult onsale(@RequestBody List<Long> ids) {
        try {
            PetService.onsale(ids);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }
    @PostMapping("/offsale")
    public jsonResult offsale(@RequestBody List<Long> ids) {
        try {
            PetService.offsale(ids);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }
}

